-- --------------------------------------------------------------------------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------
-- 
-- 					             Aquarium Race    
--  
-- --------------------------------------------------------------------------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------


	AEGIS.chatCommands.race = {adminOnly=true;};

		function AEGIS.chatCommands.race:func(player, chatMsg)

			if (XIsAdmin(player)) then
			if(player:IsDead()) then return end
                	if(player.actor:GetSpectatorMode()~=0) then return end
                	if(RaceStarted) then

	XSendBigCenterMessageToPlayer(player,"<b><i></font><font color=\"#FFFFFF\">Es gibt noch ein Rennen</font>");

        	g_gameRules:CreateExplosion(flare,weaponId,0,ChequeredFlag,g_Vectors.up,1,1,1,1,"explosions.flare.night_time_selfillum",1, 1, 1, 1); return end

-- --------------------------------------------------------------------------------------------------------------------------
-- --------------------------------------------[Fahrzeug Einstellung]--------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------

		local VehiclePaints 				= {"us","nk"};
                local CivPaints 				= {"police"};
                RacePaint 					= VehiclePaints[math.random(1, 2)]; local HoverPushx 	= 0; local HoverPushy = 0;
                local VehicleSelect, RaceVehicles 		= string.match((chatMsg.text), "^!race (.*)");
                if (string.lower(tostring(VehicleSelect))	=="1")then RaceCarType 	= "Civ_speedboat"; RaceMod 	= "RaceSpeed";
                elseif(string.lower(tostring(VehicleSelect))	=="2")then RaceCarType 	= "US_smallboat"; RaceMod 	= "Unarmed";
                elseif(string.lower(tostring(VehicleSelect))	=="3")then RaceCarType 	= "US_hovercraft"; RaceMod 	= "Unarmed";
                elseif(string.lower(tostring(VehicleSelect))	=="4")then RaceCarType 	= "Asian_patrolboat";

                else 						           RaceCarType  = "Civ_speedboat"; RaceMod = "Unarmed"; end

-- --------------------------------------------------------------------------------------------------------------------------
-- -------------------------------------------- [Race Aktivierung]-----------------------------------------------------------
-- -------------------------------------------------------------------------------------------------------------------------- 
               
                ChequeredFlag 		= {422.7097,229.3586,240.2409}; 
		CheckPoint 		= {102.9187,267.6962,240.2409};

		RaceAward = 2000;

					Script.SetTimer( 00001,function() 

				System.ExecuteCommand("x_remove_name RACER#1"); 
				System.ExecuteCommand("x_remove_name RACER#2"); 
				System.ExecuteCommand("x_remove_name RACER#3"); 
				System.ExecuteCommand("x_remove_name RACER#4");end);

					Script.SetTimer( 00010,function() 

						RaceStarted = true;
 
	XSendBigCenterMessage("<b><i><font color=\"#FFFFFF\">STARTE RACE</font>");end);

					Script.SetTimer( 01000,function()

	local pos = { x=2609.3, y=2038.29, z=49.5294 }; g_gameRules:CreateExplosion(player.id,weaponId,25,pos,g_Vectors.up,1,1,1,1,"explosions.flare.night_time_selfillum",1, 1, 1, 1); end);

-- --------------------------------------------------------------------------------------------------------------------------
-- ----------------------------------------------[Licht Aktivierung]---------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------

                			Script.SetTimer( 1500,function() 

					if(LightsOn) then return else

        local coursepos = { 439.6072,267.6962,240.2409 		}; g_gameRules:CreateExplosion(tunnelstart1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.orange_flickering"		,1, 1, 1, 1);
        local coursepos = { 406.244,267.6962,240.2409 		}; g_gameRules:CreateExplosion(tunnelstart2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.orange_flickering"		,1, 1, 1, 1);

	local coursepos = { 427.2849,363.4759,240.6257 		}; g_gameRules:CreateExplosion(courseyellow1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 412.9897,378.6602,240.6257 		}; g_gameRules:CreateExplosion(courseyellow2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 395.2893,387.0715,240.6257 		}; g_gameRules:CreateExplosion(courseyellow3,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 128.8495,390.574,240.6257 		}; g_gameRules:CreateExplosion(courseyellow4,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 109.6665,379.8167,240.6257 		}; g_gameRules:CreateExplosion(courseyellow5,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 93.2108,359.3082,240.6257 		}; g_gameRules:CreateExplosion(courseyellow6,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 96.5704,169.7992,240.6257 		}; g_gameRules:CreateExplosion(courseyellow7,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
	local coursepos = { 111.5502,152.5508,240.6257 		}; g_gameRules:CreateExplosion(courseyellow8,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
        local coursepos = { 133.0591,139.1356,240.6257 		}; g_gameRules:CreateExplosion(courseyellow9,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
        local coursepos = { 387.4941,138.2227,240.6257 		}; g_gameRules:CreateExplosion(courseyellow10,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
        local coursepos = { 414.813,147.8176,240.6257 		}; g_gameRules:CreateExplosion(courseyellow11,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);
        local coursepos = { 431.8958,170.8129,240.6257 		}; g_gameRules:CreateExplosion(courseyellow12,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.green_flickering"		,1, 1, 1, 1);

        local coursepos = { 426.3941,229.3586,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 419.1217,229.3586,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 271.5413,364.9094,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 271.5413,380.1044,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 108.0807,267.9374,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 98.1946,267.6962,240.2409		}; g_gameRules:CreateExplosion(checkpointsign2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 271.5413,155.3052,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign1,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        local coursepos = { 271.5413,144.376,240.2409 		}; g_gameRules:CreateExplosion(checkpointsign2,	mesaracing,0,coursepos,g_Vectors.up,1,1,1,1,"misc.static_lights.red_flickering"			,1, 1, 1, 1);
        


               					 LightsOn = true; 

					Script.SetTimer( 120000,function() 

					LightsOn = false; end); end end);

-- --------------------------------------------------------------------------------------------------------------------------
-- ----------------------------------------------[Fahrzeug Spawn]------------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------

					Script.SetTimer( 2000,function()

   						XAddEntity({

				class 		= RaceCarType; 
				name 		= "RACER#1";
               			orientation 	= { x=0.000967867, y=0.999596, z=0.0284151 };
               			position 	= { x=434.4946+HoverPushx, y=267.311+HoverPushy, z=239.2191 };
               			properties 	= {Modification = RaceMod; Paint = RacePaint;
               			Respawn 	= {bAbandon = 1; bRespawn = 0; bUnique = 1; nAbandonTimer = 60; nTimer = 0;};};} );

   						XAddEntity({

				class 		= RaceCarType; 
				name 		= "RACER#2";
               			orientation 	= { x=0.000967867, y=0.999596, z=0.0284151 };
               			position 	= { x=426.5237+HoverPushx, y=267.311+HoverPushy, z=239.2191 };
               			properties 	= {Modification = RaceMod; Paint = RacePaint;
               			Respawn 	= {bAbandon = 1; bRespawn = 0; bUnique = 1; nAbandonTimer = 60; nTimer = 0;};};} );

   						XAddEntity({

				class 		= RaceCarType; 
				name 		= "RACER#3";
               			orientation 	= { x=0.000967867, y=0.999596, z=0.0284151 };
               			position 	= { x=418.4946+HoverPushx, y=267.311+HoverPushy, z=239.2191 };
               			properties 	= {Modification = RaceMod; Paint = RacePaint;
               			Respawn 	= {bAbandon = 1; bRespawn = 0; bUnique = 1; nAbandonTimer = 60; nTimer = 0;};};} );

   						XAddEntity({

				class 		= RaceCarType; 
				name 		= "RACER#4";
               			orientation 	= { x=0.000967867, y=0.999596, z=0.0284151 };
               			position 	= { x=410.4946+HoverPushx, y=267.311+HoverPushy, z=239.2191 };
               			properties 	= {Modification = RaceMod; Paint = RacePaint;
               			Respawn 	= {bAbandon = 1; bRespawn = 0; bUnique = 1; nAbandonTimer = 60; nTimer = 0;};};} ); end);

-- --------------------------------------------------------------------------------------------------------------------------
-- ------------------------------------------------[Race Start]--------------------------------------------------------------
-- --------------------------------------------------------------------------------------------------------------------------

               Script.SetTimer( 05000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE STARTET IN 60 SEKUNDEN</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 07000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE STARTET IN 60 SEKUNDEN</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 25000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">Schreibe im Chat !raceportal um mitzufahren</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 27000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">Schreibe im Chat !raceportal um mitzufahren</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 35000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE STARTET IN 30 SEKUNDEN</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 37000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE STARTET IN 30 SEKUNDEN</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 45000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE PREISGELD IST 2000 PRESTIGE POINTS</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 47000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">RACE PREISGELD IST 2000 PRESTIGE POINTS</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 55000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">MACHT EUCH BEREIT</font><font color=\"#ff0000\">]</font>");end);
	       Script.SetTimer( 57000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">MACHT EUCH BEREIT</font><font color=\"#ff0000\">]</font>");end);
               Script.SetTimer( 62000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">)))</font><font color=\"#FFFFFF\">3</font><font color=\"#ff0000\">(((</font>");end);
               Script.SetTimer( 63000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">)))</font><font color=\"#FFFFFF\">2</font><font color=\"#ff0000\">(((</font>");end);
               Script.SetTimer( 64000,function() XSendBigCenterMessage("<b><i><font color=\"#ff0000\">)))</font><font color=\"#FFFFFF\">1</font><font color=\"#ff0000\">(((</font>");end);

                ----------------------------------------------------------------------------------------------------
               
                 			Script.SetTimer( 55000,function() 

	g_gameRules.game:SendTextMessage(TextMessageError, "Record Fahrer ist"..recorddriver.." mit der Bestzeit von "..Rekordtime.." und dem Fahrzeug [ "..Rekordvehicle.." ", TextMessageToAllRemote);

                		local racers 		= System.GetEntitiesByClass(RaceCarType);for i, cars in ipairs(racers) do local CarName = cars:GetName();
                      		if (CarName=="RACER#1" or CarName=="RACER#2" or CarName=="RACER#3" or CarName=="RACER#4") then local RacerId = cars:GetDriverId();
                          	if (type(RacerId) == "userdata") then driver = System.GetEntity(RacerId);
                            	if (driver) then driverName = driver:GetName(); local racerTeamId = g_gameRules.game:GetTeam(driver.id);

	XSendChatToAll(nil, "RENN FAHRER %s KOMMT ZUM RENNEN FAHRER", driverName, CarName, X_TEAM_LABEL[racerTeamId]);

        g_gameRules.game:SendTextMessage(TextMessageBig, "", TextMessageToClient, driver.id);

                               if (driver.inventory:GetCountOfClass("RepairKit")==0) then ItemSystem.GiveItem("RepairKit", driver.id, true); end end end end end end);

             				Script.SetTimer( 60010,function()
 
	g_gameRules.game:SendTextMessage(TextMessageBig, "", TextMessageToAllRemote); end);

             				Script.SetTimer( 60500,function()

 				racecountdown 			= true; end); 

					Script.SetTimer( 60525,function() 

				racecountdown 			= false; end)

                ----------------------------------------------------------------------------------------------------
                        Script.SetTimer( 64920,function() local pos = {2609.3,2038.29,49.5294}; g_gameRules:CreateExplosion(player.id,weaponId,25,pos,g_Vectors.up,1,1,1,1,"explosions.flare.night_time_selfillum",1, 1, 1, 1); end);
                        Script.SetTimer( 64940,function() local pos = {2609.3,2038.29,49.5294}; g_gameRules:CreateExplosion(player.id,weaponId,25,pos,g_Vectors.up,1,1,1,1,"misc.extremly_important_fx.celebrate",1, 1, 1, 1); end);
                        Script.SetTimer( 65000,function() timestart = _time; g_gameRules.game:SendTextMessage(TextMessageBig, "<font size=\"36\"><font color=\"#FFFFFF\">* * *</font> <font color=\"#ff0000\"> <i><b> GO - GO - GO </b></i> </font> <font color=\"#FFFFFF\">* * *</font></font>", TextMessageToAllRemote); CheckPointActive = true; end);else

	XSendBigCenterMessageToPlayer(player,"<font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end


-- --------------------------------------------------------------------------------------------------------------------------
-- >>>     						!racekill
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.racekill = {adminOnly=true;};

		function AEGIS.chatCommands.racekill:func(player, chatMsg)

					if (XIsAdmin(player)) then
                			if(player:IsDead()) then return end
                			if(player.actor:GetSpectatorMode()~=0) then return end
                			if(not RaceStarted) then 

	XSendChatToTarget(nil, player, ":::: schreib !race um ein Rennen zu starten ::::"); return end

                			RaceStarted 		= false;
                			CheckPointActive 	= false;

	XSendChatToTarget(nil, player, ":::: Aktuelle Rennen abgebrochen! ::::");else

	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>     						!raceportal
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.raceportal = {};

		function AEGIS.chatCommands.raceportal:func(player, chatMsg)

          				if(player:IsDead()) then return end
          				if(player.actor:GetSpectatorMode()~=0) then return end
             				if (player:IsOnVehicle()) then 

	XSendChatToTarget(nil, player, ""); return end

             				local x 	= player:GetWorldPos().x; 
					local y 	= player:GetWorldPos().y; 
					local z 	= player:GetWorldPos().z;

             		g_gameRules.game:SetInvulnerability(player.id, true, 10);
             		g_gameRules:CreateExplosion(player.id,wormhole,1,{x = x, y = y, z = z + 7},g_Vectors.up,1,1,1,1,"misc.sphere_energy.sphere_creation",0.5, 0.5, 0.5, 0.5);

	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#FFFFFF\">STARTE RACE PORTAL</font>");

	XSendChatToTarget(nil, player, "");

		Script.SetTimer( 6500,function() local racepos = {422.5074,240.7806,257.7527};

			g_gameRules:CreateExplosion(player.id,vanishingeffect,1,{x = x, y = y, z = z - 1},g_Vectors.up,1,1,1,1,"misc.whirlpool.explosion",0.1, 0.1, 0.1, 0.1);
			g_gameRules.game:MovePlayer(player.id,racepos,player:GetWorldAngles());
			g_gameRules:CreateExplosion(player.id,reborn,1,racepos,g_Vectors.up,1,1,1,1,"alien_special.Hunter.Pre_Self_Destruct_body",0.3, 0.3, 0.3, 0.3);

	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#FFFFFF\">RACE PORTAL KOMPLETT</font>");end);end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>							!createrace
-- --------------------------------------------------------------------------------------------------------------------------
	AEGIS.chatCommands.createrace = {
		adminOnly	=true;
		checkpoints = {};
		vehicleTypes= {};
		creating	= false;
	};
	
	function AEGIS.chatCommands.createrace:func(player, chatMsg)
		AEGIS.chatCommands.createrace.creating 		= true;
		AEGIS.chatCommands.createrace.vehicleTypes 	= {};
		AEGIS.chatCommands.createrace.checkpoints	= {};
		XSendBigCenterMessageToPlayer(player, "You can now start creating a race!");
	end
	
	AEGIS.chatCommands.addcheckpoint = {
		adminOnly	= true;
	};
	
	function AEGIS.chatCommands.addmark:func(player, chatMsg)
	    if (not AEGIS.chatCommands.createrace.creating) then
	        --ToDo: Send message to player
	        return
		end
  		local checkpoint = {
  		    pos 	= player:GetWorldPos();
  		    rot 	= player:GetWorldAngles();
  		    effect  = "misc.static_lights.green_flickering";
  		    };
		table.insert(AEGIS.chatCommands.createrace.checkpoints, checkpoint)
	end